package com.zenithmind.document.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import jakarta.validation.constraints.*;
import java.util.List;

/**
 * 文档评分创建DTO
 * 遵循单一职责原则：专注于评分创建数据传输
 * 遵循开闭原则：通过验证注解支持扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档评分创建DTO")
public class DocumentRatingCreateDTO {

    @Schema(description = "文档ID", required = true, example = "doc123456")
    @NotBlank(message = "文档ID不能为空")
    private String documentId;

    @Schema(description = "用户ID", required = true, example = "user123456")
    @NotBlank(message = "用户ID不能为空")
    private String userId;

    @Schema(description = "评分", required = true, example = "4.5", minimum = "1", maximum = "5")
    @NotNull(message = "评分不能为空")
    @DecimalMin(value = "1.0", message = "评分不能小于1.0")
    @DecimalMax(value = "5.0", message = "评分不能大于5.0")
    private Double rating;

    @Schema(description = "评论内容", example = "这是一个很好的文档，内容详细且易懂")
    @Size(max = 2000, message = "评论内容不能超过2000字符")
    private String comment;

    @Schema(description = "评分维度", example = "[{\"dimension\": \"内容质量\", \"score\": 5}, {\"dimension\": \"易读性\", \"score\": 4}]")
    private String ratingDimensions;

    @Schema(description = "评分标签", example = "有用,详细,清晰")
    private String ratingTags;

    @Schema(description = "评分类型", example = "GENERAL", allowableValues = {"GENERAL", "EXPERT", "PEER_REVIEW", "AUTOMATED"})
    private String ratingType;

    @Schema(description = "评分来源", example = "WEB", allowableValues = {"WEB", "MOBILE", "API", "SYSTEM"})
    private String ratingSource;

    @Schema(description = "是否匿名", example = "false")
    private Boolean isAnonymous;

    @Schema(description = "推荐给他人", example = "true")
    private Boolean recommendToOthers;

    @Schema(description = "推荐理由", example = "内容全面，适合新手学习")
    private String recommendReason;

    @Schema(description = "使用场景", example = "学习参考")
    private String usageScenario;

    @Schema(description = "阅读时长（分钟）", example = "30")
    private Integer readingDuration;

    @Schema(description = "文档版本", example = "v1.2")
    private String documentVersion;

    @Schema(description = "评分权重", example = "1.0")
    @DecimalMin(value = "0.1", message = "评分权重不能小于0.1")
    @DecimalMax(value = "2.0", message = "评分权重不能大于2.0")
    private Double ratingWeight;

    @Schema(description = "评分置信度", example = "0.95")
    @DecimalMin(value = "0.0", message = "置信度不能小于0.0")
    @DecimalMax(value = "1.0", message = "置信度不能大于1.0")
    private Double confidence;

    @Schema(description = "情感倾向", example = "POSITIVE", allowableValues = {"POSITIVE", "NEUTRAL", "NEGATIVE"})
    private String sentiment;

    @Schema(description = "关键词", example = "质量,内容,实用")
    private String keywords;

    @Schema(description = "改进建议", example = "可以增加更多实例")
    private String improvementSuggestions;

    @Schema(description = "相关文档推荐", example = "doc789,doc101")
    private String relatedDocuments;

    @Schema(description = "评分上下文", example = "{\"project\": \"ProjectA\", \"team\": \"TeamB\"}")
    private String ratingContext;

    @Schema(description = "设备信息", example = "PC")
    private String deviceInfo;

    @Schema(description = "IP地址", example = "192.168.1.100")
    private String ipAddress;

    @Schema(description = "用户代理", example = "Mozilla/5.0...")
    private String userAgent;

    @Schema(description = "扩展属性", example = "{\"category\": \"技术文档\", \"difficulty\": \"中级\"}")
    private String extendedProperties;

    @Schema(description = "备注", example = "通过搜索找到的文档")
    private String remarks;

    /**
     * 创建简单评分
     */
    public static DocumentRatingCreateDTO createSimpleRating(String documentId, String userId, Double rating) {
        DocumentRatingCreateDTO dto = new DocumentRatingCreateDTO();
        dto.setDocumentId(documentId);
        dto.setUserId(userId);
        dto.setRating(rating);
        dto.setRatingType("GENERAL");
        dto.setRatingSource("WEB");
        dto.setIsAnonymous(false);
        dto.setRatingWeight(1.0);
        dto.setConfidence(0.8);
        return dto;
    }

    /**
     * 创建详细评分
     */
    public static DocumentRatingCreateDTO createDetailedRating(String documentId, String userId, Double rating, 
                                                              String comment, String tags) {
        DocumentRatingCreateDTO dto = createSimpleRating(documentId, userId, rating);
        dto.setComment(comment);
        dto.setRatingTags(tags);
        dto.setConfidence(0.9);
        return dto;
    }

    /**
     * 创建专家评分
     */
    public static DocumentRatingCreateDTO createExpertRating(String documentId, String userId, Double rating, 
                                                            String comment, String dimensions) {
        DocumentRatingCreateDTO dto = new DocumentRatingCreateDTO();
        dto.setDocumentId(documentId);
        dto.setUserId(userId);
        dto.setRating(rating);
        dto.setComment(comment);
        dto.setRatingDimensions(dimensions);
        dto.setRatingType("EXPERT");
        dto.setRatingSource("WEB");
        dto.setIsAnonymous(false);
        dto.setRatingWeight(1.5);
        dto.setConfidence(0.95);
        return dto;
    }

    /**
     * 创建匿名评分
     */
    public static DocumentRatingCreateDTO createAnonymousRating(String documentId, String userId, Double rating, String comment) {
        DocumentRatingCreateDTO dto = createSimpleRating(documentId, userId, rating);
        dto.setComment(comment);
        dto.setIsAnonymous(true);
        dto.setConfidence(0.7);
        return dto;
    }

    /**
     * 验证DTO数据
     */
    public boolean isValid() {
        if (documentId == null || documentId.trim().isEmpty()) return false;
        if (userId == null || userId.trim().isEmpty()) return false;
        if (rating == null || rating < 1.0 || rating > 5.0) return false;
        
        // 验证评论长度
        if (comment != null && comment.length() > 2000) return false;
        
        // 验证权重范围
        if (ratingWeight != null && (ratingWeight < 0.1 || ratingWeight > 2.0)) return false;
        
        // 验证置信度范围
        if (confidence != null && (confidence < 0.0 || confidence > 1.0)) return false;
        
        return true;
    }

    /**
     * 是否为高分评价
     */
    public boolean isHighRating() {
        return rating != null && rating >= 4.0;
    }

    /**
     * 是否为低分评价
     */
    public boolean isLowRating() {
        return rating != null && rating <= 2.0;
    }

    /**
     * 是否有评论
     */
    public boolean hasComment() {
        return comment != null && !comment.trim().isEmpty();
    }

    /**
     * 是否有标签
     */
    public boolean hasTags() {
        return ratingTags != null && !ratingTags.trim().isEmpty();
    }

    /**
     * 是否有维度评分
     */
    public boolean hasDimensions() {
        return ratingDimensions != null && !ratingDimensions.trim().isEmpty();
    }

    /**
     * 是否推荐给他人
     */
    public boolean isRecommended() {
        return recommendToOthers != null && recommendToOthers;
    }

    /**
     * 获取评分标签列表
     */
    public String[] getRatingTagArray() {
        if (ratingTags == null || ratingTags.trim().isEmpty()) {
            return new String[0];
        }
        return ratingTags.split(",");
    }

    /**
     * 获取关键词列表
     */
    public String[] getKeywordArray() {
        if (keywords == null || keywords.trim().isEmpty()) {
            return new String[0];
        }
        return keywords.split(",");
    }

    /**
     * 获取相关文档列表
     */
    public String[] getRelatedDocumentArray() {
        if (relatedDocuments == null || relatedDocuments.trim().isEmpty()) {
            return new String[0];
        }
        return relatedDocuments.split(",");
    }

    /**
     * 计算评分质量分数
     */
    public double calculateQualityScore() {
        double score = 0.0;
        
        // 基础评分权重 40%
        if (rating != null) {
            score += 0.4;
        }
        
        // 评论权重 30%
        if (hasComment()) {
            score += 0.3;
            // 评论长度加分
            if (comment.length() > 50) {
                score += 0.1;
            }
        }
        
        // 标签权重 15%
        if (hasTags()) {
            score += 0.15;
        }
        
        // 维度评分权重 10%
        if (hasDimensions()) {
            score += 0.1;
        }
        
        // 推荐理由权重 5%
        if (recommendReason != null && !recommendReason.trim().isEmpty()) {
            score += 0.05;
        }
        
        return Math.min(score, 1.0);
    }

    /**
     * 评分类型枚举
     */
    public enum RatingType {
        GENERAL("普通评分"),
        EXPERT("专家评分"),
        PEER_REVIEW("同行评议"),
        AUTOMATED("自动评分");

        private final String description;

        RatingType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 评分来源枚举
     */
    public enum RatingSource {
        WEB("网页端"),
        MOBILE("移动端"),
        API("API接口"),
        SYSTEM("系统自动");

        private final String description;

        RatingSource(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 情感倾向枚举
     */
    public enum Sentiment {
        POSITIVE("正面"),
        NEUTRAL("中性"),
        NEGATIVE("负面");

        private final String description;

        Sentiment(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}
